#include "../import/head.h"
#include "helper_http.h"
#include "../import/import.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_net/head.h"
#include "http_proc.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{

	//////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 

	helper_http::helper_http()
	{
	}

	helper_http::~helper_http()
	{
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// proc

	int helper_http::on_recv( xos::i_buf * pBuf, data * pData )
	{
		int ret = 0;

		tcp * pTcp = pData->m_pTcp;
		session * pSession = pTcp->m_pSession;

		if( ( 0 == ret ) && !pTcp->m_pParseRequest )
		{
			ret = g_pSysData->m_os.m_pHttpMgr->create( xos_http::OBJ_TYPE_PARSE_REQUEST, (void **)&pTcp->m_pParseRequest );
		}

		if( ( 0 == ret ) && !pTcp->m_pComposeRet )
		{
			ret = g_pSysData->m_os.m_pHttpMgr->create( xos_http::OBJ_TYPE_COMPOSE_RETURN, (void **)&pTcp->m_pComposeRet );
		}

		if( 0 == ret )
		{
			pTcp->m_pParseRequest->set_request_type( xos_http::REQUEST_TYPE_HTTP );
			pTcp->m_pComposeRet->set_request_type( xos_http::REQUEST_TYPE_HTTP );
			pTcp->m_pParseRequest->set_user_data( pTcp );
			pTcp->m_pComposeRet->set_user_data( pTcp );
			pSession->add_ref( 1 );
			ret = pTcp->m_pParseRequest->parse( pBuf );
			pBuf = 0;
		}

		return ret;
	}

	int helper_http::proc( xos::i_msg * pMsg )
	{
		int ret = 0;

		int nMsg = pMsg->get_int( 0, 0 );

		switch( nMsg )
		{
		case xos_http::MSG_TYPE_PARSE_REQUEST_SUCCESS:
			{
				ret = on_parse_request_success( pMsg );
			}
			break;
		case xos_http::MSG_TYPE_PARSE_REQUEST_FAILED:
			{
				ret = on_parse_request_failed( pMsg );
			}
			break;
		case xos_http::MSG_TYPE_COMPOSE_RET_SUCCESS:
			{
				ret = on_compose_ret_success( pMsg );
			}
			break;
		case xos_http::MSG_TYPE_COMPOSE_RET_FAILED:
			{
				ret = on_compose_ret_failed( pMsg );
			}
			break;
		case xos_http::MSG_TYPE_COMPOSE_RET_DATA:
			{
				ret = on_compose_ret_data( pMsg );
			}
			break;
		case xos_http::MSG_TYPE_COMPOSE_RET_DATA_END:
			{
				ret = on_compose_ret_data_end( pMsg );
			}
			break;
		default:
			{
				ret = 1;
			}
			break;
		}

		if( pMsg )
		{
			pMsg->release();
			pMsg = 0;
		}

		return ret;
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 

	int helper_http::on_parse_request_success( xos::i_msg * pMsg )
	{
		int ret = 0;

		tcp * pTcp = (tcp *)pMsg->get_void( 0, 0 );
		session * pSession = pTcp->m_pSession;

		if( 0 == ret )
		{
			ret = pSession->sub_ref( 1 );
		}

		if( 0 == ret )
		{
			http_proc obj;
			ret = obj.proc( pSession );
		}

		return ret;
	}

	int helper_http::on_parse_request_failed( xos::i_msg * pMsg )
	{
		int ret = 0;

		tcp * pTcp = (tcp *)pMsg->get_void( 0, 0 );
		session * pSession = pTcp->m_pSession;

		if( 0 == ret )
		{
			ret = pSession->sub_ref( 2 );
		}

		return ret;
	}

	int helper_http::on_compose_ret_success( xos::i_msg * pMsg )
	{
		int ret = 0;
		return ret;
	}

	int helper_http::on_compose_ret_failed( xos::i_msg * pMsg )
	{
		int ret = 0;

		tcp * pTcp = (tcp *)pMsg->get_void( 0, 0 );
		session * pSession = pTcp->m_pSession;

		if( 0 == ret )
		{
			ret = pSession->sub_ref( 2 );
		}

		return ret;
	}

	int helper_http::on_compose_ret_data( xos::i_msg * pMsg )
	{
		int ret = 0;

		tcp * pTcp = (tcp *)pMsg->get_void( 0, 0 );
		xos::i_buf * pBuf = pMsg->get_buf( 0, 0 );

		if( 0 == ret )
		{
			data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
			p->init();
			p->m_pTcp = pTcp;
			p->m_pBuf = pBuf;
			net_tcp obj;
			ret = obj.tcp_post_send( p );
			pBuf = 0;
			p = 0;
		}

		if( pBuf )
		{
			pBuf->release();
			pBuf = 0;
		}

		return ret;
	}

	int helper_http::on_compose_ret_data_end( xos::i_msg * pMsg )
	{
		int ret = 0;

		tcp * pTcp = (tcp *)pMsg->get_void( 0, 0 );
		session * pSession = pTcp->m_pSession;
		xos::i_buf * pBuf = pMsg->get_buf( 0, 0 );

		if( 0 == ret )
		{
			ret = pSession->sub_ref( 1 );
		}

		if( 0 == ret )
		{
			data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
			p->init();
			p->m_pTcp = pTcp;
			p->m_pBuf = pBuf;
			net_tcp obj;
			ret = obj.tcp_post_send( p );
			pBuf = 0;
			p = 0;
		}

		if( ( 0 == ret ) && pTcp->m_pParseRequest )
		{
			pTcp->m_pParseRequest->release();
			pTcp->m_pParseRequest = 0;
		}

		if( ( 0 == ret ) && pTcp->m_pComposeRet )
		{
			pTcp->m_pComposeRet->release();
			pTcp->m_pComposeRet = 0;
		}

		if( pBuf )
		{
			pBuf->release();
			pBuf = 0;
		}

		return ret;
	}

} // xos_sgsj
